package com.zyw.dev.zyw.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.zyw.dev.zyw.dao.ZJWBeans;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by misl on 16/8/1.
 */
public class SMSTools {

    private static Uri SMS_INBOX = Uri.parse("content://sms/");

    //    Gas concentration 14% Wind speed 29.9m/s Illumination 0.00Lx Humidity 00％RH Temperature 00C
    public static final String[] SMS_SUB_TEXT = {"% ", "m/s","Lx","%RH","C"};

    public static ZJWBeans getZJWBeanBySMSBody(Date date,String body){
        ZJWBeans beans = new ZJWBeans();
        String[] values = new String[5];
        for (int i = 0;i < SMS_SUB_TEXT.length;++i){
            String subText = SMS_SUB_TEXT[i];
            String value = "0";
            if (body.contains(subText)){
                String temp = body.substring(0,body.indexOf(subText));
                value = temp.substring(temp.lastIndexOf(" "));
            }

            values[i] = value;
        }
        beans.setZJWBeans(date,values);
        return beans;
    }

    public static void getSmsFromPhone(Context context) {
        ContentResolver cr = context.getContentResolver();
        String[] projection = new String[] { "body" };//"_id", "address", "person",, "date", "type
        String where = " address = '1066321332' AND date >  "
                + (System.currentTimeMillis() - 10 * 60 * 1000);
        Cursor cur = cr.query(SMS_INBOX, projection, where, null, "date desc");
        if (null == cur)
            return;
        if (cur.moveToNext()) {
            String number = cur.getString(cur.getColumnIndex("address"));//手机号
            String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
            String body = cur.getString(cur.getColumnIndex("body"));
            //这里我是要获取自己短信服务号码中的验证码~~
            Pattern pattern = Pattern.compile(" [a-zA-Z0-9]{10}");
            Matcher matcher = pattern.matcher(body);
            if (matcher.find()) {
                String res = matcher.group().substring(1, 11);

            }
        }
    }

    public static Cursor getSMSFromPhone(Context context){
        ContentResolver cr = context.getContentResolver();
        String[] projection = new String[] { "_id","body", "address", "person", "date" };//"_id", "address", "person",, "date", "type
        String where = " body like '%Gas%'";
        Cursor cur = cr.query(SMS_INBOX, projection, where, null, "date desc");

        if (null == cur){
            return null;
        }

        while (cur.moveToNext()) {
            String number = cur.getString(cur.getColumnIndex("address"));//手机号
            String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
            String body = cur.getString(cur.getColumnIndex("body"));
            String date = cur.getString(cur.getColumnIndex("date"));
            Log.e("TAG",name+"->"+number+":"+body+" = "+date);
        }
        cur.moveToFirst();
        return cur;
    }

    public static String getDataFormat(Date date){
        SimpleDateFormat fmt = new SimpleDateFormat("yy/MM/dd HH:mm");
        return fmt.format(date);
    }
}

//
//    private SmsObserver smsObserver;
//
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.app_login);
//        smsObserver = new SmsObserver(this, smsHandler);
//        getContentResolver().registerContentObserver(SMS_INBOX, true,
//                smsObserver);
//
//    }
//    public Handler smsHandler = new Handler() {
//        //这里可以进行回调的操作
//        //TODO
//
//    };
//class SmsObserver extends ContentObserver {
//
//    public SmsObserver(Context context, Handler handler) {
//        super(handler);
//    }
//
//    @Override
//    public void onChange(boolean selfChange) {
//        super.onChange(selfChange);
//        //每当有新短信到来时，使用我们获取短消息的方法
//        getSmsFromPhone();
//    }
//}